/*
 * Students:
 * Massimiliano Rango - 610826
 * Martina Benedetto - 610962
 * 
 * A.A 2011/2012
 * 
 */ 

import java.awt.Color;
import java.awt.Label;
import java.awt.TextField;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * This class represents the application's graphic interface 
 * 
 * @version 1.0 27 Dec 2011
 * @author Martina Benedetto 
 *
 */

public class CloneFinderGUI implements GUIInterface {
	
	private Client m_client;
	private JFrame m_f;
	private JPanel m_panel1;
	private JPanel m_panel2;
	private JPanel m_panel3;
	private JPanel m_panel4;
	private JPanel m_panel5;
	private JButton m_start;
	private JButton m_stop;
	private JTextArea m_info1;
	private JScrollPane m_scroll1;
	private JTextArea m_info2;
	private JScrollPane m_scroll2;
	private Label m_infoSend;
	private Label m_infoSim;
	private Label m_RMI;
	private Label m_HTTP;
	private TextField m_RMIField;
	private TextField m_HTTPField;
	
	public CloneFinderGUI() {
		m_f = new JFrame("Kill the Clone!");
		m_panel1 = new JPanel();
		m_panel2 = new JPanel();
		m_panel3 = new JPanel();
		m_panel4 = new JPanel();
		m_panel5 = new JPanel();
		m_start = new JButton("Start");
		m_stop = new JButton("Stop");
		m_info1 = new JTextArea(10,10);
		m_scroll1 = new JScrollPane(m_info1);
		m_info2 = new JTextArea(10,10);
		m_scroll2 = new JScrollPane(m_info2);
		m_infoSend = new Label("Info sent to RMI Server:   ");
		m_infoSim = new Label("Info about the simulation:");
		m_RMI = new Label("RMI Address:");
		m_HTTP = new Label("Configuration File (HTTP Address):");
		m_RMIField = new TextField("http://localhost", 50);
		m_HTTPField = new TextField("http://localwww.math.unipd.it"
				+ "/~conti/teaching/PCD1112/project_config.txt",
				50);
		
		m_f.setSize(1000, 700);
		m_panel1.setLayout(new GridLayout(2, 2, 10, 20));
		m_panel1.add(m_RMI);
		m_panel1.add(m_RMIField);
		m_panel1.add(m_HTTP);
		m_panel1.add(m_HTTPField);
		m_f.add(m_panel1, BorderLayout.NORTH);
		
		m_panel2.setLayout(new GridLayout(2, 2, 10, 15));
		m_panel2.add(new Label(""));
		m_panel2.add(new Label(""));
		m_panel2.add(m_infoSend);
		m_panel2.add(m_infoSim);
		
		m_panel3.setLayout(new GridLayout(1, 2, 10, 15));
		m_panel3.add(m_scroll1);
		m_panel3.add(m_scroll2);
		m_info1.setLineWrap(true);
		m_info2.setLineWrap(true);
		m_info1.setEditable(false);       
		m_info2.setEditable(false);
		
		m_panel4.setLayout(new FlowLayout());
		m_panel4.add(m_start);
		m_panel4.add(m_stop);
		m_f.add(m_panel4, BorderLayout.SOUTH);
		
		m_panel5.setLayout(new BorderLayout());
		m_panel5.add(m_panel2, BorderLayout.NORTH);
		m_panel5.add(m_panel3);
		m_f.add(m_panel5, BorderLayout.CENTER);
		
		m_start.setEnabled(true);
		m_stop.setEnabled(false);
		
		m_start.addActionListener(new ClickStart());
		m_stop.addActionListener(new ClickStop());
		
		m_f.setVisible(true);
	}
	
	/**
	 * 
	 * This class implements the functioning of the button start, in order 
	 * to start the simulation once the button is clicked
	 *
	 */
	class ClickStart implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			m_info1.setText("");
			m_info2.setText("");        
			m_RMIField.setBackground(null);
			m_HTTPField.setBackground(null);
			
			String RMI = m_RMIField.getText();
			if (RMI.isEmpty()) {
				m_RMIField.setBackground(Color.red);
			}
			
			String HTTP = m_HTTPField.getText();
			if (HTTP.isEmpty()) {
				m_HTTPField.setBackground(Color.red);
			}
			
			try {
				m_client = new Client(HTTP, RMI, CloneFinderGUI.this);
				m_client.start();
				m_start.setEnabled(false);
				m_stop.setEnabled(true);
			} catch (Exception ex) {
				m_info2.append(ex.toString());
			}
		}
	}
	
	/**
	 * 
	 * This class implements the functioning of the button stop, in order to
	 * stop the simulation once the button is clicked
	 *
	 */
	class ClickStop implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			if(m_stop.isEnabled()) {	/*if the simulation has started*/
				m_client.setClickStop();
				m_stop.setEnabled(false);
			}
			
		}
	}
	
	/**
	 * 
	 * @param info is the string that we want to be shown in the text-area 
	 * concerning the info sent to RMIServer
	 */
	public void printRMIArea(String info) {
		m_info1.append(info);
	}
	
	/**
	 * 
	 * @param info is the string that we want to be shown in the text-area 
	 * concerning the info about the simulation's parameters
	 */
	public void printInfoArea(String info) {
		m_info2.append(info);
	}
	
	public void clientFinished() {
		m_start.setEnabled(true);
		m_stop.setEnabled(false);
        }
	
	public static void main(String args[]) {
		CloneFinderGUI gui = new CloneFinderGUI();
		gui.m_f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	}
}
